# Author ： chosen-yn

import pandas as pd
import matplotlib.pyplot as plt
from mplfinance.original_flavor import candlestick_ohlc

data = {
    'Date': ['2024-07-01', '2024-07-02', '2024-07-03', '2024-07-04', '2024-07-05'],
    'Open': [100, 105, 110, 106, 200],
    'High': [100, 105, 115, 120, 112],
    'Low': [90, 98, 105, 108, 100],
    'Close': [112, 110, 108, 116, 102]
}

# 将数据转换为DateFrame
df = pd.DataFrame(data)

# 将日期列转换为日期时间格式
df['Date'] = pd.to_datetime(df['Date'])

# 将DataFrame的索引设置为日期列
df.set_index('Date', inplace=True)

# 创建OHLC数据
ohlc_data = df[['Open', 'High', 'Low', 'Close']].reset_index()

# 将日期列转换为Unix时间戳
ohlc_data['Date'] = ohlc_data['Date'].apply(lambda x: x.timestamp())

# 使用mapltfinance库绘制K线图
fig, ax = plt.subplots(figsize=(12, 6))
candlestick_ohlc(ax, ohlc_data.values, width=0.6, colorup='g', colordown='r')

# 设置图标标题和轴标签
ax.set_title('股票价格图')
ax.set_xlabel('Date')
ax.set_ylabel('Price')

plt.show()
